/**
 * 提供操作json object图文对象的接口
 */

/**
 * 根据ID修改object中的对象
 */
function update(id,object){
	for(var o in pack_json){
		if(o.id==id){
			//删除之后，重新加入pack_json_array
			
		}
	}
}

function autoSaveItems(){
	//获取当前编辑的图文
	var editing_msg=$.serializeObject($('#single_form'));
	//alert(editing_msg.id+"   "+editing_msg.title);
	for(var i=0,n=pack_json.length;i<n;i++){
		if(pack_json[i].id==editing_msg.id){
			for (var key in editing_msg) {
				//console.log(key+'===>'+pack_json[i][key]+"   ||||  "+editing_msg[key]);过滤特殊字符
				if(key=='content'){pack_json[i][key]=editor.getContent();continue;}
				pack_json[i][key]=editing_msg[key];
			}
			break;
		}
	}
}
function init_array(){
	editor.addListener( 'ready', function( editor ) {
			for(var i=1;i<=2;i++){
				insertObj('N'+i,i);
			}
	 });
}
function insertObj(id,seq){
	var o={};
	o.id=id;
	if(typeof(wChatId)=='undefined'||wChatId==null)wChatId=0;
	o.wid=wChatId;
	o.state=1;
	o.cover='';
	o.seq=seq;
	if(typeof(packId)=='undefined'||packId==null)packId=0;
	o.packId=packId;
	o.date=new Date().getTime();
	o.title='';
	o.author='';
	o.brief='';
	o.content='';
	o.sourceUrl='';
	pack_json.push(o);
	//同时给表单赋值
	for (var key in o) {
		if(key=='cover'){$("#cover").val(o[key]);$('#preview_img').attr('src',o[key]);continue;}
		if(key=='content'){
			if(editor!=null&&typeof(editor)!='undefined'){editor.setContent(o[key]);continue;}
		}
		$('#'+key).val(o[key]);//console.log($('#'+key));
    }
}

function deleteObj(id){
	var array=new Array();
	for(var i=0,n=pack_json.length;i<n;i++){
		if(pack_json[i].id==id)continue;
		array.push(pack_json[i]);
	}
	pack_json=array;
	array=null;
}
